package com.cms.content.problem.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.framework.utils.BeanUtilEx;
import com.cms.framework.utils.page.PageView;
import com.cms.content.problem.dao.ProblemDAO;
import com.cms.content.problem.pojo.Problem;
import com.cms.content.problem.pojo.dto.ProblemDTO;
import com.cms.content.problem.service.ProblemService;
@Service
public class ProblemServiceImpl implements ProblemService {
	@Autowired
	ProblemDAO problemDAO;
	@Override
	public boolean saveProblem(Problem problem) {
		if(problem.getId()==null){
			return problemDAO.insert(problem)>0;
		}else{
			return problemDAO.updateByPrimaryKey(problem)>0;
		}
	}

	@Override
	public boolean deleteById(Integer Id) {
		return problemDAO.deleteByPrimaryKey(Id)>0;
	}

	@Override
	public Problem findById(Integer Id) {
		return problemDAO.selectByPrimaryKey(Id);
	}

	@Override
	public List<Problem> findList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageView<ProblemDTO> findListByPage(PageView<ProblemDTO> pageView,
			ProblemDTO problemDTOParams) {
		List<Problem> list=problemDAO.selectByPage(pageView, problemDTOParams);
		List<ProblemDTO> dtoList=new ArrayList<ProblemDTO>();
		for(Problem problem:list){
			ProblemDTO problemDTO=new ProblemDTO();
			try {
				BeanUtilEx.copyProperties(problemDTO, problem);
				//friendlinkDTO.setPcUrl("pcUrl");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			dtoList.add(problemDTO);
		}
		pageView.setRecords(dtoList);
		return pageView;
	}

}
